local w = require('utils.win')

return  {
    "jonahfang/betterTerm.nvim",
    keys = {
        {
            mode = { 'n' },
            '<M-t>',
            function()
                local b = require('betterTerm')
                local last_tab = _G.BetterTermLastActiveTab
                b.open()
                if last_tab then
                    vim.defer_fn(function()
                        b.switch_to(last_tab)
                    end, 100)
                end
            end,
            desc = 'Open BetterTerm and switch to last tab',
        },
        {
            mode = { 't' },
            '<M-b>',
            function()
                local b = require('betterTerm')
                local last_tab = _G.BetterTermLastLastActiveTab
                local cur_tab = _G.BetterTermLastActiveTab
                if not last_tab then
                    last_tab = 'z0'
                end
                if cur_tab == last_tab then
                   if cur_tab == 'z1' then
                      b.switch_to('z0')
                   else
                      b.switch_to('z1')
                   end
                else
                   b.switch_to(last_tab)
                end
            end,
            desc = 'Switch back to last tab of terminal',
        },
        {
            mode = { 't' },
            '<M-f>',
            function()
                local b = require('betterTerm')
                local cur_tab = _G.BetterTermLastActiveTab
                if not cur_tab then
                    cur_tab = 'z0'
                end
                if cur_tab == 'z0' then
                  b.switch_to('z1')
                end
                if cur_tab == 'z1' then
                  b.switch_to('z2')
                end
                if cur_tab == 'z2' then
                  b.switch_to('z3')
                end
                if cur_tab == 'z3' then
                  b.switch_to('z4')
                end
            end,
            desc = 'switch to the right tab of terminal',
        },
    },
    config = function()
        require('betterTerm').setup {
            prefix = "z",
            position = "bot", -- vertical,bot
            size = 35,
            startInserted = true,
            show_tabs = true,
            new_tab_mapping = "zt",      -- Create new terminal
            jump_tab_mapping = "z$tab",  -- Jump to tab terminal
            active_tab_hl = "BetterTermActive",
            inactive_tab_hl = "CursorLine",
            new_tab_hl = "BetterTermSymbol", -- Highlight group for new term
            new_tab_icon = "+"              -- Icon for new term
        }
    end
}
--EOP
